package com.bitin.adorn.modules.user.api;

import com.bitin.adorn.common.orm.domain.Email;
import com.bitin.adorn.common.orm.domain.Phone;
import com.bitin.adorn.common.payload.ApiResult;
import com.bitin.adorn.common.payload.Pagination;
import com.bitin.adorn.modules.user.domain.User;
import com.bitin.adorn.modules.user.request.PagingRequest;
import com.bitin.adorn.modules.user.request.UserRegisterRequest;
import com.bitin.adorn.modules.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制层
 *
 * @author Mr.Lu
 * @since 2024/12/11 17:26
 */
@Tag(name = "02.用户接口")
@RestController
@RequestMapping("/api/v1/user")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @Operation(summary = "全部用户")
    @GetMapping("/all")
    public ApiResult<List<User>> AllUser() {
        return ApiResult.success(this.userService.findAll());
    }

    @Operation(summary = "用户分页列表")
    @GetMapping("/page")
    public ApiResult<Pagination<User>> page(PagingRequest pagingRequest) {
        return ApiResult.success(this.userService.page(pagingRequest));
    }

    @Operation(summary = "用户注册")
    @PostMapping("/user/register")
    public ApiResult<User> save(@RequestBody UserRegisterRequest request) {
        User user = User.builder()
            .username(request.getUsername())
            .phone(Phone.build(request.getMobile()))
            .email(Email.build(request.getEmail()))
            .password(request.getPassword())
            .gender(request.getGender())
            .build();
        return ApiResult.success(this.userService.save(user));
    }


}
